package com.pocket.insight.base.sql.dialect;

import com.pocket.insight.base.mql.TypedVal;
import com.pocket.insight.base.utils.SqlUtil;
import org.apache.commons.lang3.StringUtils;

/**
 * 描述：
 *
 * @author shuize
 * @create 2024/12/20
 */
public class SqlserverDialect extends BaseDialect {

    /********************************* 1. 标识符差异配置 *********************************/
    @Override
    public String getLeftQuoteChar() {
        return "[";
    }

    @Override
    public String getRightQuoteChar() {
        return "]";
    }

    @Override
    public String quoteString(String s) {
        return String.format("N'%s'", this.escString(s));
    }

    @Override
    public String quoteSingle(String s) {
        return String.format("[%s]", StringUtils.replace(StringUtils.replace(s, "]", ""), "[", ""));
    }

    @Override
    public String unquoteSingleEntity(String s) {
        s = SqlUtil.removeRepeatedSymbolAtHeadAndTail(s, '[');
        return SqlUtil.removeRepeatedSymbolAtHeadAndTail(s, ']');
    }

    /********************************* 2. TODO *********************************/

    @Override
    public TypedVal setPage(TypedVal query, Long offset, Long limit) {
        if (query == null) {
            return new TypedVal();
        } else if (StringUtils.isEmpty(query.getStr())) {
            return query;
        } else {
            String sql = query.getStr();
            if (offset != null && offset > 0L) {
                sql = sql + " offset " + offset + " rows fetch next " + limit + " rows only";
            } else if (StringUtils.startsWithIgnoreCase(sql, "SELECT distinct")) {
                sql = StringUtils.replaceOnceIgnoreCase(sql, "SELECT distinct", "SELECT distinct top " + limit);
            } else {
                sql = StringUtils.replaceOnceIgnoreCase(sql, "SELECT", "SELECT top " + limit);
            }
            return new TypedVal(sql);
        }
    }

    @Override
    public Object jodaToNativeParseFormat(String jodaFormat) {
        if (jodaFormat.startsWith("N'")) {
            jodaFormat = jodaFormat.substring(2, jodaFormat.length() - 1);
        }
        switch (jodaFormat) {
            case "MM/dd/yy":
                return 1;
            case "yy.MM.dd":
                return 2;
            case "dd/MM/yy":
                return 3;
            case "dd.MM.yy":
                return 4;
            case "dd-MM-yy":
                return 5;
            case "MM-dd-yy":
                return 10;
            case "yy/MM/dd":
                return 11;
            case "yyyy-MM-dd HH:mm:ss":
            case "yyyy-MM-dd":
            case "yyyy":
                return 20;
            case "yyMMdd":
                return 12;
            case "yyyy-MM-dd HH:mm:ss.SSS":
                return 21;
            case "MM/dd/yyyy":
                return 101;
            case "yyyy.MM.dd":
                return 102;
            case "dd/MM/yyyy":
                return 103;
            case "dd.MM.yyyy":
                return 104;
            case "dd-MM-yyyy":
                return 105;
            case "MM-dd-yyyy":
                return 110;
            case "yyyy/MM/dd":
                return 111;
            case "yyyyMMdd":
                return 112;
            default:
                return null;
        }
    }
}
